{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "from dyn_tree import *\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {},
   "outputs": [],
   "source": [
    "#define an outside function\n",
    "def report(x,*args):\n",
    "    #report node's name in each level\n",
    "    #print(x[args[0]])\n",
    "    try:\n",
    "        print('res=',args[1])\n",
    "        args[1].append(x[args[0]])\n",
    "    except:\n",
    "        pass\n",
    "    print('res=',args[1])\n",
    "    return args"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {},
   "outputs": [],
   "source": [
    "# initialize  Dyn_tree instance\n",
    "a=Dyn_tree('Tom')\n",
    "b=Dyn_tree('John')\n",
    "c=Dyn_tree('Lucy')\n",
    "d=Dyn_tree('Mary')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'Tom',\n",
       " 'attr': 'node',\n",
       " 'children': [{'name': 'John', 'attr': 'node'},\n",
       "  {'name': 'Mary', 'attr': 'node'}]}"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a.add_children(b)\n",
    "a.add_children(d)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {},
   "outputs": [],
   "source": [
    "#when b add child c,a had a grandson automatically\n",
    "b.add_children(c)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'name': 'Tom',\n",
       " 'attr': 'node',\n",
       " 'children': [{'name': 'John',\n",
       "   'attr': 'node',\n",
       "   'children': [{'name': 'Lucy', 'attr': 'node'}]},\n",
       "  {'name': 'Mary', 'attr': 'node'}]}"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "a"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "res= []\n",
      "res= ['Tom']\n",
      "res= ['Tom']\n",
      "res= ['Tom', 'John']\n",
      "res= ['Tom', 'John']\n",
      "res= ['Tom', 'John', 'Lucy']\n",
      "res= ['Tom', 'John', 'Lucy']\n",
      "res= ['Tom', 'John', 'Lucy', 'Mary']\n"
     ]
    }
   ],
   "source": [
    "res=a.dosomething(report,'name',[])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "('name', ['Tom', 'John', 'Lucy', 'Mary'])"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "res"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {},
   "outputs": [],
   "source": [
    "def graph_viz(x,g):\n",
    "    g.node(x['name'])\n",
    "    try:\n",
    "        x['children']\n",
    "        if type(x['children'])==list:\n",
    "            [g.edge(x['name'],y['name']) for y in x['children'] ]\n",
    "\n",
    "\n",
    "            return [x.graph_viz(g) for x in  x['children']]\n",
    "        else:\n",
    "            g.edge(x['name'],x['children']['name'])\n",
    "\n",
    "            return x['children'].graph_viz(g)\n",
    "    except:\n",
    "        return g"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {},
   "outputs": [],
   "source": [
    "from graphviz import Digraph"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/svg+xml": [
       "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"no\"?>\r\n",
       "<!DOCTYPE svg PUBLIC \"-//W3C//DTD SVG 1.1//EN\"\r\n",
       " \"http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd\">\r\n",
       "<!-- Generated by graphviz version 2.38.0 (20140413.2041)\r\n",
       " -->\r\n",
       "<!-- Title: test_digraph Pages: 1 -->\r\n",
       "<svg width=\"143pt\" height=\"188pt\"\r\n",
       " viewBox=\"0.00 0.00 143.14 188.00\" xmlns=\"http://www.w3.org/2000/svg\" xmlns:xlink=\"http://www.w3.org/1999/xlink\">\r\n",
       "<g id=\"graph0\" class=\"graph\" transform=\"scale(1 1) rotate(0) translate(4 184)\">\r\n",
       "<title>test_digraph</title>\r\n",
       "<polygon fill=\"white\" stroke=\"none\" points=\"-4,4 -4,-184 139.145,-184 139.145,4 -4,4\"/>\r\n",
       "<!-- Tom -->\r\n",
       "<g id=\"node1\" class=\"node\"><title>Tom</title>\r\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"67.2474\" cy=\"-162\" rx=\"28.6953\" ry=\"18\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"67.2474\" y=\"-158.3\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">Tom</text>\r\n",
       "</g>\r\n",
       "<!-- John -->\r\n",
       "<g id=\"node2\" class=\"node\"><title>John</title>\r\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"29.2474\" cy=\"-90\" rx=\"27.8951\" ry=\"18\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"29.2474\" y=\"-86.3\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">John</text>\r\n",
       "</g>\r\n",
       "<!-- Tom&#45;&gt;John -->\r\n",
       "<g id=\"edge1\" class=\"edge\"><title>Tom&#45;&gt;John</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M58.4339,-144.765C53.8297,-136.283 48.0924,-125.714 42.926,-116.197\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"45.86,-114.266 38.0131,-107.147 39.7081,-117.606 45.86,-114.266\"/>\r\n",
       "</g>\r\n",
       "<!-- Mary -->\r\n",
       "<g id=\"node3\" class=\"node\"><title>Mary</title>\r\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"105.247\" cy=\"-90\" rx=\"29.795\" ry=\"18\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"105.247\" y=\"-86.3\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">Mary</text>\r\n",
       "</g>\r\n",
       "<!-- Tom&#45;&gt;Mary -->\r\n",
       "<g id=\"edge2\" class=\"edge\"><title>Tom&#45;&gt;Mary</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M76.0609,-144.765C80.5994,-136.404 86.2388,-126.016 91.347,-116.606\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"94.5229,-118.092 96.2178,-107.633 88.3709,-114.752 94.5229,-118.092\"/>\r\n",
       "</g>\r\n",
       "<!-- Lucy -->\r\n",
       "<g id=\"node4\" class=\"node\"><title>Lucy</title>\r\n",
       "<ellipse fill=\"none\" stroke=\"black\" cx=\"29.2474\" cy=\"-18\" rx=\"29.4969\" ry=\"18\"/>\r\n",
       "<text text-anchor=\"middle\" x=\"29.2474\" y=\"-14.3\" font-family=\"Times New Roman,serif\" font-size=\"14.00\">Lucy</text>\r\n",
       "</g>\r\n",
       "<!-- John&#45;&gt;Lucy -->\r\n",
       "<g id=\"edge3\" class=\"edge\"><title>John&#45;&gt;Lucy</title>\r\n",
       "<path fill=\"none\" stroke=\"black\" d=\"M29.2474,-71.6966C29.2474,-63.9827 29.2474,-54.7125 29.2474,-46.1124\"/>\r\n",
       "<polygon fill=\"black\" stroke=\"black\" points=\"32.7475,-46.1043 29.2474,-36.1043 25.7475,-46.1044 32.7475,-46.1043\"/>\r\n",
       "</g>\r\n",
       "</g>\r\n",
       "</svg>\r\n"
      ],
      "text/plain": [
       "<graphviz.dot.Digraph at 0x415c5f0>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "g = Digraph('test_digraph')\n",
    "a.dosomething(graph_viz,g)\n",
    "\n",
    "g"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 4
}
